package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Ads;
import com.ejuornal.bean.vo.AdsVO;
import com.ejuornal.dao.AdsDao;

@Component("adsDao")
public class AdsDaoImpl extends DaoSupport<Ads> implements AdsDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	public void batchDelete(String sql, final int... ids) {
		super.batchDelete(sql, ids);
	}

	public Ads findById(int adsId) {
		String sql = "select * from t_ads where adsId = ?";
		List<Ads> adss = super.getJdbcTemplate().query(sql,
				new Object[] { adsId }, new RowMapper<Ads>() {
					public Ads mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Ads ads = new Ads();
						ads.setAdsId(rs.getInt("adsId"));
						ads.setAdsName(rs.getString("adsName"));
						ads.setAdsPosId(rs.getInt("adsPosId"));
						ads.setBeginDate(rs.getDate("beginDate"));
						ads.setEndDate(rs.getDate("endDate"));
						ads.setPicSrc(rs.getString("picSrc"));
						ads.setRemarks(rs.getString("remarks"));
						ads.setUrl(rs.getString("url"));
						return ads;
					}
				});
		if(adss.isEmpty()) {
		    return null;
		} else {
		    return adss.get(0);
		}
	}

	@Override
	public AdsVO findByIdVo(int adsId) {
		String sql = "select a.*,p.adsPos from t_ads a INNER JOIN t_ads_Pos p where a.adsPosId = p.adsPosId and a.adsId = ?";
		List<AdsVO> adsVOs = super.getJdbcTemplate().query(sql, 
				new Object[]{adsId},new RowMapper<AdsVO>(){
			public AdsVO mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				AdsVO adsVO = new AdsVO(); 
				adsVO.setAdsId(rs.getInt("adsId"));
				adsVO.setAdsName(rs.getString("adsName"));
				adsVO.setAdsPosId(rs.getInt("adsPosId"));
				adsVO.setBeginDate(rs.getDate("beginDate"));
				adsVO.setEndDate(rs.getDate("endDate"));
				adsVO.setPicSrc(rs.getString("picSrc"));
				adsVO.setRemarks(rs.getString("remarks"));
				adsVO.setAdsPos(rs.getString("adsPos"));
				adsVO.setUrl(rs.getString("url"));
				return adsVO;
			}
		});
		if(adsVOs.isEmpty()) {
		    return null;
		} else {
		    return adsVOs.get(0);
		}
	}

    @Override
    public boolean updateLoginImg(String url) {
        String sql = "update t_ads set picSrc=? where adsId=1";
        int num = super.getJdbcTemplate().update(sql, new Object[]{url});
        if(num > 0) {
            return true;
        }
        return false;
    }

	@Override
	public Ads getAdsByPosIdAndTime(int posId) {
		String sql = "select * from t_ads a where a.adsPosId = ? " +
				"and TO_DAYS(NOW()) >= TO_DAYS(a.beginDate)" +
				"and TO_DAYS(NOW()) < TO_DAYS(a.endDate)";
		List<Ads> adss = super.getJdbcTemplate().query(sql,
				new Object[] { posId }, new RowMapper<Ads>() {
					public Ads mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Ads ads = new Ads();
						ads.setAdsId(rs.getInt("adsId"));
						ads.setAdsName(rs.getString("adsName"));
						ads.setAdsPosId(rs.getInt("adsPosId"));
						ads.setBeginDate(rs.getDate("beginDate"));
						ads.setEndDate(rs.getDate("endDate"));
						ads.setPicSrc(rs.getString("picSrc"));
						ads.setRemarks(rs.getString("remarks"));
						ads.setUrl(rs.getString("url"));
						return ads;
					}
				});
		if(adss.isEmpty()) {
		    return null;
		} else {
		    return adss.get(0);
		}
	}
}
